Dplyr Exercises - Solutions

Perform the following operations using only the dplyr library. We will be reviewing the following operations:

  • filter() (and slice())
  • arrange()
  • select() (and rename())
  • distinct()
  • mutate() (and transmute())
  • summarise()
  • sample_n() and sample_frac()
In [6]:
library(dplyr)

We will use the mtcars dataframe for this exercise!

In [9]:
head(mtcars)
Out[9]:
mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Datsun 71022.84108933.852.3218.611141
Hornet 4 Drive21.462581103.083.21519.441031
Hornet Sportabout18.783601753.153.4417.020032
Valiant18.162251052.763.4620.221031

Return rows of cars that have an mpg value greater than 20 and 6 cylinders.

In [8]:
filter(mtcars,mpg>20,cyl==6)
Out[8]:
mpgcyldisphpdratwtqsecvsamgearcarb
12161601103.92.6216.460144
22161601103.92.87517.020144
321.462581103.083.21519.441031

Reorder the Data Frame by cyl first, then by descending wt.

In [14]:
head(arrange(mtcars,cyl,desc(wt)))
Out[14]:
mpgcyldisphpdratwtqsecvsamgearcarb
124.44146.7623.693.19201042
222.84140.8953.923.1522.91042
321.441211094.112.7818.61142
421.54120.1973.72.46520.011031
522.84108933.852.3218.611141
632.4478.7664.082.219.471141

Select the columns mpg and hp

In [13]:
head(select(mtcars,mpg,hp))
Out[13]:
mpghp
Mazda RX421110
Mazda RX4 Wag21110
Datsun 71022.893
Hornet 4 Drive21.4110
Hornet Sportabout18.7175
Valiant18.1105

Select the distinct values of the gear column.

In [17]:
distinct(select(mtcars,gear))
Out[17]:
gear
14
23
35

Create a new column called "Performance" which is calculated by hp divided by wt.

In [19]:
head(mutate(mtcars,Performance=hp/wt))
Out[19]:
mpgcyldisphpdratwtqsecvsamgearcarbPerformance
12161601103.92.6216.46014441.98473
22161601103.92.87517.02014438.26087
322.84108933.852.3218.61114140.08621
421.462581103.083.21519.44103134.21462
518.783601753.153.4417.02003250.87209
618.162251052.763.4620.22103130.34682

Find the mean mpg value using dplyr.

In [20]:
summarise(mtcars,avg_mpg=mean(mpg))
Out[20]:
avg_mpg
120.09062

Use pipe operators to get the mean hp value for cars with 6 cylinders.

In [22]:
mtcars %>% filter(cyl==6) %>% summarise(avg_hp = mean(hp))
Out[22]:
std_hp
124.26049

Great Job!